package com.cheng.example.book.java8actual.chapter03;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/*
 * @author 程帅
 * @createTime: 2020/11/19 13:49
 * @Created: with IntelliJ IDEA.
 */
public class Client3_1 {

    public static void main(String[] args) {

//        Comparator<Apple> byWeight = new Comparator<Apple>() {
//            public int compare(Apple a1, Apple a2){
//                return a1.getWeight().compareTo(a2.getWeight());
//            }
//        };

        Comparator<Apple> byWeight = (Apple a1, Apple a2) -> a1.getWeight().compareTo(a2.getWeight());

        Weight w1 = new Weight();
        w1.setNum(1);
        Weight w2 = new Weight();
        w2.setNum(2);
        Weight w3 = new Weight();
        w3.setNum(3);

        Apple a1 = new Apple();
        a1.setWeight(w1);
        Apple a2 = new Apple();
        a2.setWeight(w2);
        Apple a3 = new Apple();
        a3.setWeight(w3);

        List<Apple> list = new ArrayList<>();
        list.add(a1);
        list.add(a3);
        list.add(a2);

        System.out.println(list);
        Collections.sort(list, byWeight);
        System.out.println(list);

    }
}
